package com.qianchang.microfinance.setting;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.alert.DimengSingleButtonAlertDialog;
import com.qianchang.microfinance.view.login.ForgetActivityOne;
import com.qianchang.microfinance.view.login.LoginActivity;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.MD5Util;
import com.dm.library.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 修改登录密码
 * 修 改 人: zhoulai
 * 修改时间: 2017/6/30 16:22
 */


public class ModifyPasswordActivity extends BaseActivity {


    //    private DimengImageContentBtnAlertDialog mDialog_finishInfo;
    private final static String TAG = "ModifyPasswordActivity";
    @BindView(R.id.et_input_pre_pwd)
    EditText etInputPrePwd;
    @BindView(R.id.iv_prepwd_visible)
    CheckBox ivPrepwdVisible;
    @BindView(R.id.ll_checkcode)
    LinearLayout llCheckcode;
    @BindView(R.id.et_input_pwd)
    EditText etInputPwd;
    @BindView(R.id.iv_pwd_visible)
    CheckBox ivPwdVisible;
    @BindView(R.id.btn_sure)
    Button btnSure;
    @BindView(R.id.ll_forget_pwd)
    LinearLayout llForgetPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modity_password);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.titleModifyLoginPassword));
    }

    @Override
    protected void initData() {
        super.initData();
        ivPrepwdVisible.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int length = etInputPrePwd.getText().length();
                if (isChecked) {
                    etInputPrePwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    etInputPrePwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                etInputPrePwd.setSelection(length);
            }
        });
        ivPwdVisible.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int length = etInputPwd.getText().length();
                if (isChecked) {
                    etInputPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    etInputPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                etInputPwd.setSelection(length);
            }
        });
    }


    @OnClick({R.id.btn_sure, R.id.ll_forget_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_sure:
                updatePwd();
                break;
            case R.id.ll_forget_pwd:
                Intent intent = new Intent(ModifyPasswordActivity.this, ForgetActivityOne.class);
                intent.putExtra(SettingTradPasswordActivity.IS_TRAD_PWD, false);
                startActivity(intent);
                break;
        }
    }

    /**
     * 修改密码
     */
    private void updatePwd() {
        String prePwd = etInputPrePwd.getText().toString().trim();

        String pwd = etInputPwd.getText().toString().trim();

        if (!FormatUtil.isValidPassword(prePwd, "")) {
            ToastUtil.getInstant().show(ModifyPasswordActivity.this, getString(R.string.msg_pwd_length_too_short));
            return;
        }

        if (!FormatUtil.isValidPassword(pwd, "")) {
            ToastUtil.getInstant().show(ModifyPasswordActivity.this, getString(R.string.msg_pwd_length_too_short));
            return;
        }

        show();
        HttpParams params = new HttpParams();
        params.put("oldPassword", MD5Util.string2MD5(
                prePwd + ConstantAppInfo.Password.PASSWORD_SUFFIX));
        params.put("newPassword", MD5Util.string2MD5(
                pwd + ConstantAppInfo.Password.PASSWORD_SUFFIX));//
        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.ApiURL.UPDATE_PWD_BY_PRE_PWD, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(JsonResult<String> response) {
                Log.i(TAG, response.toString());
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    DimengSingleButtonAlertDialog singleButtonAlertDialog = DimengSingleButtonAlertDialog.newInstance("", "密码修改成功，请重新登录！");
                    singleButtonAlertDialog.setOnDialogClickListener(new DimengSingleButtonAlertDialog.OnDialogClickListener() {
                        @Override
                        public void agree() {
                            toLogin();
                        }
                    });
                    singleButtonAlertDialog.show(getSupportFragmentManager(), getClass().getSimpleName());
                } else {
                    ToastUtil.getInstant().show(ModifyPasswordActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(ModifyPasswordActivity.this, msg);// getString(R.string.msg_indentity_code_send_fail));
            }

            @Override
            public void onConnectFailure(String msg) {
                ToastUtil.getInstant().show(ModifyPasswordActivity.this, msg);
            }

        });
    }


    private void toLogin() {
        DMApplication.getInstance().setUserLoginInfo(null);
        startActivity(LoginActivity.class);

    }
}
